<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="150px">
      <el-form-item label="异常分类" prop="typeId">
        <el-cascader clearable v-model="queryParams.typeId" :options="sortType" :props="{
          expandTrigger: 'hover',
          value: 'id',
          label: 'excepDesc',
          children: 'childList',
        }" style="width: 100%" />
      </el-form-item>
      <el-form-item label="SN编码" prop="snCode">
        <el-input v-model="queryParams.snCode" placeholder="请输入SN编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工单编码" prop="workorderCode">
        <el-input v-model="queryParams.workorderCode" placeholder="请输入工单编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品编码" prop="productCode">
        <el-input v-model="queryParams.productCode" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="规格型号" prop="productSpc">
        <el-input v-model="queryParams.productSpc" placeholder="请输入规格型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="处理日期">
        <el-date-picker v-model="queryParams.time" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="异常分类" width="200" align="center" prop="excepDesc" />
      <el-table-column label="异常状态" align="center" prop="flowStatusName" />
      <el-table-column label="SN编码" align="center" prop="snCode" />
      <el-table-column label="工单编码" align="center" prop="workorderCode" />
      <el-table-column label="物料编码" align="center" prop="productCode" />
      <el-table-column label="物料名称" align="center" prop="productName" />
      <el-table-column label="规格型号" align="center" prop="productSpc" />
      <el-table-column label="批次号" align="center" prop="batchCode" />
      <el-table-column label="批号名称" align="center" prop="batchName" />
      <el-table-column label="工单数量" align="center" prop="quantity" />
      <el-table-column label="跟进人" align="center" prop="followName" />
      <el-table-column label="跟进时间" align="center" prop="followTime" />
      <el-table-column label="处理人" align="center" prop="handleName" />
      <!-- <el-table-column label="处理时间" align="center" prop="handleTime" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <span v-show="scope.row.source == '' || scope.row.source == null">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, false)" :disabled="scope.row.flowStatus != 2 && scope.row.flowStatus != 3" v-hasPermi="['exception:handle:edit']">处理</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, true)">处理意见</el-button>
          </span>

          <!--add begin by zyj 2025-01-15 IQC质检不合格流程: 系统发起提报-30E-计划-采购-品质部经理-->
          <!-- <el-button :disabled="scope.row.source != '' || scope.row.flowStatus == 3" size="mini" type="text"
            icon="el-icon-edit" @click="handleAudit(scope.row)" v-hasPermi="['exception:handle:audit']">审核</el-button> -->
          <!--add end by zyj 2025-01-15 IQC质检不合格流程: 系统发起提报-30E-计划-采购-品质部经理-->
          <!-- edit 更换审核按钮使用判定条件 -->
          <el-button v-show="scope.row.source != '' && scope.row.flowStatus == 3 && currentUser == scope.row.auditPerson" size="mini" type="text" icon="el-icon-edit" @click="handleAudit(scope.row)">审核</el-button>
          <!-- edit end 更换审核按钮使用判定条件 -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:record:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改异常填报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <!-- add begin by zyj 2024/11/05 显示多人处理意见-->
      <el-table :data="handleList">
        <el-table-column label="处理人" align="center" prop="handleName" />
        <el-table-column label="处理时间" align="center" prop="handleTime" />
        <el-table-column label="处理意见" align="center" prop="content" />
      </el-table>
      <!-- add end by zyj 2024/11/05 显示多人处理意见-->
      <el-form ref="form" :model="form" v-if="!isJustShowRemark" :rules="rules" label-width="150px">
        <el-form-item label="指导处理意见：" style="flex: 0 0 50%">
          {{ guideRemark ? guideRemark : "无" }}
        </el-form-item>
        <el-form-item label="处理方案" prop="content" style="flex: 0 0 50%">
          <el-input type="textarea" rows="5" v-model="form.content" placeholder="请输入处理方案" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!isJustShowRemark">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消'</el-button>
      </div>
    </el-dialog>
    <AuditDialog @success="auditSuccess" ref="auditDialog"></AuditDialog>
  </div>
</template>

<script>
import {
  listRecord,
  getRecord,
  delRecord,
  addRecord,
  updateRecord,
  getTreeList,
  getWorkorderBySnCode,
  handleSubmit,
  getHandleList,
  getHandleListAll,
  getProgrammeInfo,
  editProgrammeInfo,
  handleException
} from "@/api/system/handle";
import AuditDialog from "../components/auditDialog.vue";
export default {
  name: "ExceptionHandle",
  dicts: ["sys_flow_status"],
  components: {
    AuditDialog,
  },
  data() {
    return {
      currentUser: this.$store.state.user.name,//当前登录人
      //add begin by zyj 2024/11/05 显示多人处理意见
      handleList: [],
      guideRemark: null,
      //add end by zyj 2024/11/05 显示多人处理意见
      selection: [],
      typeVal: null,
      // 遮罩层
      loading: true,
      updateId: null,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 异常填报表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isJustShowRemark: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeId: null,
        snCode: null,
        workorderId: null,
        oneLevelId: null,
        twoLevelId: null,
        threeLevelId: null,
        judgeBy: null,
        judgeTime: null,
        followBy: null,
        followTime: null,
        handleBy: null,
        handleTime: null,
        confirmBy: null,
        confirmTime: null,
        abnormalWorkinghours: null,
        flowStatus: null,
      },
      // 表单参数
      form: {
        id: null,
        content: null,
      },
      // 表单校验
      rules: {
        content: [
          { required: true, message: "处理方案不能为空", trigger: "blur" },
        ],
      },
      sortType: [],
    };
  },
  created() {
    this.getList();
    this.getTreeList();
  },
  methods: {
    //add begin by zyj 2025-01-15 IQC质检不合格流程: 系统发起提报-30E-计划-采购-品质部经理
    auditSuccess() {
      this.getList();
    },
    handleAudit(row) {
      this.$refs.auditDialog.show(row.id);
    },
    //add end by zyj 2025-01-15 IQC质检不合格流程: 系统发起提报-30E-计划-采购-品质部经理
    //add begin by zyj 2024/11/05 显示多人处理意见
    getHandleList(row) {
      const param = {
        exceptionId: row.id,
      };
      this.guideRemark = row.guideRemark;
      let getHandleListPromise = this.isJustShowRemark ? getHandleListAll(param) : getHandleList(param)
      getHandleListPromise.then((res) => {
        console.log(res.data.list)
        this.handleList = res.data.list ? res.data.list : [];
        this.form.content = res.data.content;
        this.open = true;
        this.title = "处理方案";
      });
    },
    //add end by zyj 2024/11/05 显示多人处理意见
    getTreeList() {
      getTreeList().then((res) => {
        let data = res.data ? res.data : [];
        data.forEach((item) => {
          item.childList.forEach((item2) => {
            delete item2.childList;
          });
        });
        this.sortType = data;
      });
    },
    /** 查询异常填报列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.typeId && this.queryParams.typeId.length) {
        this.queryParams.typeId = this.queryParams.typeId[1];
      }
      listRecord(this.queryParams).then((response) => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;

      this.reset();
      this.$nextTick(() => {
        this.isJustShowRemark = false;
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        content: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams, "=====queryParams===");
      if (this.queryParams.time) {
        this.queryParams["handleTimeBegin"] = this.queryParams.time[0];
        this.queryParams["handleTimeEnd"] = this.queryParams.time[1];
      } else {
        this.queryParams["handleTimeBegin"] = null;
        this.queryParams["handleTimeEnd"] = null;
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.time = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection = selection;
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row, isJustShowRemark) {
      this.isJustShowRemark = isJustShowRemark;
      this.reset();
      const id = row.id || this.ids[0];
      this.updateId = row.id || this.ids[0];
      const param = {
        id,
      };
      //edit begin by zyj 2024/11/05 多人处理异常
      // getProgrammeInfo(param).then((res) => {
      //   this.form = res.data ? res.data : {};
      //   this.open = true;
      //   this.title = "处理方案";
      // });
      this.getHandleList(row);
      //edit end by zyj 2024/11/05 多人处理异常
    },
    /** 提交按钮 */
    submitForm() {
      if (this.isJustShowRemark) {
        this.cancel();
        return;
      }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          //edit begin by zyj 2024/11/05 处理异常
          handleException({ content: this.form.content, exceptionId: this.updateId })
            .then(res => {
              this.open = false;
              this.$modal.msgSuccess("处理成功");
              this.getList();
            });

          // if (this.form.id) {
          //   const param = {
          //     id: this.form.id,
          //     content: this.form.content,
          //   };


          //   editProgrammeInfo(param).then((res) => {
          //     this.$modal.msgSuccess("处理成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // } else {
          //   const param = {
          //     id: this.updateId,
          //     content: this.form.content,
          //   };
          //   handleSubmit(param).then((response) => {
          //     this.$modal.msgSuccess("处理成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // }
          //edit end by zyj 2024/11/05 处理异常
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除异常填报编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRecord(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/record/export",
        {
          ...this.queryParams,
        },
        `record_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.sort .el-cascader {
  width: 100%;
}
</style>
